﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hosting.Class;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

namespace HostingDomainWebsite
{
    public partial class AdminLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["username"] = null;
        }

        protected bool isValid()
        {
            string patternUsername = @"[a-zA-Z0-9]{5,50}";
            Regex rUsername = new Regex(patternUsername);
            Match m1 = rUsername.Match(txtUsername.Value);

            string patternPassword = @"[a-zA-Z0-9@!#$\'%^&*`~]{5,50}";
            Regex rPassword = new Regex(patternPassword);
            Match m2 = rPassword.Match(txtUsername.Value);

            if (m1.Success && m2.Success)
            {
                return true;
            }
            return false;

        }

        protected void Login_Click(object sender, EventArgs e)
        {
            //System.Threading.Thread.Sleep(6000);
            if (isValid())
            {
                Users login = new Users();
                int isLogin = login.isLogin(txtUsername.Value, txtPassword.Value);
                if (isLogin == 1)
                {
                    Session["username"] = txtUsername.Value;
                    Response.Redirect("AdminPage.aspx");
                }
                else if(isLogin == 0)
                {
                    sloginMessage.InnerHtml = "<lable style='color:red;'>Username or Password is not correct!</label>";
                    divNotification.Attributes["Class"] = "notification error png_bg";
                }
                else if (isLogin == 2)
                {
                    sloginMessage.InnerHtml = "<lable style='color:red;'>Your account has been blocked!</label>";
                    divNotification.Attributes["Class"] = "notification error png_bg";
                }
                else
                {
                    sloginMessage.InnerHtml = "<lable style='color:red;'>There is an error!</label>";
                    divNotification.Attributes["Class"] = "notification error png_bg";
                }
            }
            else 
            {
                sloginMessage.InnerHtml = "<lable style='color:red;'>Username or Password is not valid!</label>";
                divNotification.Attributes["Class"] = "notification error png_bg";
            }
        }
    }
}